﻿<%@ WebHandler Language="C#" Class="hesuan_pandian_suishi" %>

using System;
using System.Web;
using KeaoBase;
using ServiceStack.Redis;
using System.Configuration;
using System.Collections.Generic;
using System.Data;

public class hesuan_pandian_suishi : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            if (KeaoRedis.ContainsKey(key))
            {
                context.Response.Write(KeaoRedis.ReadString(key));
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                string requestType = string.Format("{0}", context.Request.QueryString["requestType"]);
                switch (requestType)
                {
                    case "1":
                        string kaishi = string.Format("{0}", context.Request.QueryString["kaishi"]);
                        string jieshu = string.Format("{0}", context.Request.QueryString["jieshu"]);
                        string zhandian = string.Format("{0}", context.Request.QueryString["zhandian"]);
                        object[] SqlParams1 = null;
                        SqlParams1 = new object[] {
                               m_DB.CreateParamter("@kaishishijian",string.Format("{0} 00:00:00",kaishi),false),
                               m_DB.CreateParamter("@jieshushijian",string.Format("{0} 23:59:59",jieshu),false),
                               m_DB.CreateParamter("@zhandian",string.Format("{0}",zhandian),false)
                        };
                        DataSet Ds1 = m_DB.ExecuteTableProcedure("P_HeSuan_SuiShi", SqlParams1);
                        if (Ds1.Tables.Count > 0&&Ds1.Tables[0].Rows.Count>0)
                        {
                            for (int i = 0; i < Ds1.Tables[0].Rows.Count; i++)
                            {
                                if (i == Ds1.Tables[0].Rows.Count - 1)
                                {
                                    foreach (DataRow heJiRow in Ds1.Tables[1].Rows)
                                    {
                                        if (heJiRow["wuliaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["xinghaomingcheng"]))
                                        {
                                            DataRow newRow = Ds1.Tables[0].NewRow();
                                            newRow["xinghaomingcheng"] = "合计：";
                                            newRow["pandianxiaohao"] = heJiRow["pandianxiaohao"];
                                            newRow["shejixiaohao"] = heJiRow["shejixiaohao"];
                                            newRow["yingkui"] = heJiRow["yingkui"];
                                            i++;
                                            Ds1.Tables[0].Rows.InsertAt(newRow, i);
                                            break;
                                        }
                                    }
                                    break;
                                }

                                if (!Ds1.Tables[0].Rows[i]["wuliaomingcheng"].Equals(Ds1.Tables[0].Rows[i + 1]["wuliaomingcheng"]) || !Ds1.Tables[0].Rows[i]["xinghaomingcheng"].Equals(Ds1.Tables[0].Rows[i + 1]["xinghaomingcheng"]))
                                {
                                    foreach (DataRow heJiRow in Ds1.Tables[1].Rows)
                                    {
                                        if (heJiRow["wuliaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["wuliaomingcheng"]) && heJiRow["xinghaomingcheng"].Equals(Ds1.Tables[0].Rows[i]["xinghaomingcheng"]))
                                        {
                                            DataRow newRow = Ds1.Tables[0].NewRow();
                                            newRow["xinghaomingcheng"] = "合计：";
                                            newRow["pandianxiaohao"] = heJiRow["pandianxiaohao"];
                                            newRow["shejixiaohao"] = heJiRow["shejixiaohao"];
                                            newRow["yingkui"] = heJiRow["yingkui"];
                                            i++;
                                            Ds1.Tables[0].Rows.InsertAt(newRow, i);
                                            break;
                                        }
                                    }
                                }
                            }

                            string jsonStr = KeaoUtils.Dt2Json(Ds1.Tables[0]);
                            context.Response.Write(jsonStr);
                            KeaoRedis.WriteString(key, jsonStr,600);
                        }
                        else
                        {
                            context.Response.Write("[]");
                        }
                        break;
                    case "2":
                        string hesuanid = string.Format("{0}", context.Request.QueryString["hesuanid"]);
                        object[] createParams = new object[] {
                            m_DB.CreateParamter("@hesuanID",hesuanid,false)
                        };
                        DataSet biaoDuanDt=m_DB.ExecuteTableProcedure("P_HeSuanShiGongXinXiByHeSuanID", createParams);
                        if (biaoDuanDt.Tables.Count > 0)
                        {
                            string biaoDuanJsonStr = KeaoUtils.Dt2Json(biaoDuanDt.Tables[0]);
                            context.Response.Write(biaoDuanJsonStr);
                        }
                        break;
                    case "3":
                        string kaipanriqi = string.Format("{0} 00:00:00", context.Request.QueryString["kaipanriqi"]);
                        string peibizhandian = string.Format("{0}", context.Request.QueryString["zhandian"]);
                        string peibigongcheng = string.Format("{0}", context.Request.QueryString["gongcheng"]);
                        string peibiCondition = m_DB.CreateAndCondition("peibiriqi", DataValueCommonType.日期时间, kaipanriqi, "", SearchConditionType.等于);
                        peibiCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, peibizhandian, "", SearchConditionType.等于);
                        peibiCondition += m_DB.CreateAndCondition("gongcheng", DataValueCommonType.字符串, peibigongcheng, "", SearchConditionType.等于);
                        Dictionary<string, string> peibiOrderbys = new Dictionary<string, string>();
                        peibiOrderbys.Add("guanlianwuliao", "asc");
                        DataTable peibiDt = m_DB.GetDataTableByCondition("tbl_peihebi", peibiCondition,peibiOrderbys);
                        string peibiJsonStr = KeaoUtils.Dt2Json(peibiDt);
                        context.Response.Write(peibiJsonStr);
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("盘点碎石核算异常。#{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}